---@class LUltiity : Object
local MobileSelGenMuty = require "packages/lang/MobileSelGenMuty"
local HobbyGenPool = require "packages/lang/HobbyGenPool"
local LUltiity = {}
--是否同一阵营
LUltiity.Camps = {lord = 1,loyalist = 1,rebel = 2,renegade = 3}
function LUltiity:isSameCamp(player,target)
  return LUltiity.Camps[player.role] == LUltiity.Camps[target.role]
end

function LUltiity:putEquipHandle(who,type,id,proposer,skiname)
  local room = who.room
  local eid = who:getEquipment(type)
  room:moveCards({
    from = who.id,
    ids = {id},
    toArea = Card.Processing,
    moveReason = fk.ReasonJustMove,
    proposer = who.id,
    skillName = skiname,
  })
  local move3 = {
    ids = {id},
    fromArea = Card.Processing,
    to = who.id,
    toArea = Card.PlayerEquip,
    moveReason = fk.ReasonJustMove,
    proposer = proposer or who.id,
    skillName = skiname,
  }
  if eid ~= nil then
    local move2 = {
      ids = {eid},
      from = who.id,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonJustMove,
    }
    room:moveCards(move2, move3)
  else
    room:moveCards(move3)
  end
end
--获得有牌区域对应的牌名
function LUltiity:getPlayerCardNameIds(player,area,cardnames)
  local cardlist = {}
  local arecards = area == "h" and player.player_cards[Player.Hand] or player:getCardIds(area)

  for _,name in ipairs(cardnames) do
    cardlist[name] = {}
  end
  for _,cid in ipairs(arecards) do
    local card = Fk:getCardById(cid)
    if cardlist[card.trueName] then
      table.insert(cardlist[card.trueName],cid)
    end
  end
  return cardlist
end
--获得受视为锁定技影响的卡牌
function LUltiity:getCardsByFilterSkill(player,card)
  local fiterskills = Fk:currentRoom().status_skills[FilterSkill] or Util.DummyTable
  if #fiterskills == 0 then return card end
  local cardid = card.id
  for _, skill in ipairs(fiterskills) do
    if skill:cardFilter(card,player) then
      local cd = skill:viewAs(card,player)
      if cd then
        cd.originid = cardid
        return cd
      end 
    end
  end
  return card
end
--计算玩家手牌(拥有某种牌名)防具体力等信息
function LUltiity:calPlayerInfo(player,cardname,from,card)
  local amor = player:getEquipment(Card.SubtypeArmor)
  local amorvalue = 0
  if amor then
    amor = Fk:getCardById(amor)
    amorvalue = amor.trueName ~= "silver_lion" and 1 or 0
  end
  local handsvalue = cardname and #table.filter(player.player_cards[Player.Hand],function(cid) return Fk:getCardById(cid).trueName == cardname end) or 0
  local hp_shield_v = player.hp * 0.35 + (player.shield or 0)*0.35
  local totalcal = amorvalue + handsvalue + hp_shield_v + (player.subhp and player.hp or 0) + (player.NoIdea and player:NoIdea(from,card) or 0)
  return totalcal
end
--获得玩家的下一个同阵营或者不同阵营玩家
function LUltiity:getNextCampAlive(player,iscamp,filtertars)
  local room = Fk:currentRoom()
  local alives = filtertars or room.alive_players
  if #alives == 1 then
    return alives[1]
  elseif #alives == 0 then
    return player.rest > 0 and player.next.rest > 0 and player.next or player
  end
  local ret = player
  for _ = 1, #room.alive_players do
    ret = ret.next
    local searchret = iscamp and (LUltiity:isSameCamp(player,ret)) or (not LUltiity:isSameCamp(player,ret))
    local isfilter = true
    if filtertars then
      if not table.contains(filtertars,ret.id) then
        isfilter = false
      end
    end
    -- p(ret.general)
    -- p(ret.id)
    -- p(searchret)
    -- p(isfilter)
    -- p(filtertars)
    -- dbg()
    if ret ~= player and searchret and isfilter then
      return ret
    end
  end
  return player
end

--手杀换将界面选将
function LUltiity:mobileChangeGen(player,selnum,needkingdom)
  local room = player.room
  local command,game_type = MobileSelGenMuty.COMMAND,MobileSelGenMuty.GAMETYPE
  local game = Fk.mini_games[game_type]
  if not game then return end

  local selgen_cards = {}
  for _,pr in ipairs(room.players) do
    if pr.role == "loyalist" then
      table.insert(selgen_cards,"请选将")
    end
  end
  local seatmap = {}
  for i=1,#selgen_cards do
    seatmap[i] = 2*i-1
  end
  local playerpool = HobbyGenPool:getgenPool("MN_XLKJ",MobileSelGenMuty.POOLTYPE.Players)
  local playergens
  if not needkingdom then
    playergens = table.random(playerpool,selnum)
  else
    playergens = {}
    local fkgens = Fk.generals
    for _,gen in ipairs(playerpool) do
      if fkgens[gen].kingdom == needkingdom then
        table.insert(playergens,gen)
      end
    end
  end
  for _,pr in ipairs(room.players) do
    if table.contains(playergens,pr.general) then
      table.removeOne(playergens,pr.general)
    end
  end
  if needkingdom then
    playergens = table.random(playergens,selnum)
  end
  local data = {seat = player.seat,gens = playergens,seats = seatmap,realnum = 1,selected_cards = selgen_cards}
  player.mini_game_data = { type = game_type, data = data}

  player.request_data = json.encode(player.mini_game_data)
  player.default_reply = game.default_choice and json.encode(game.default_choice(player, player.mini_game_data.data)) or ""

  room:notifyMoveFocus({player}, "AskForGeneral")
  room:doBroadcastRequest(command, {player})

  player.mini_game_data = nil
  local general = json.decode(player.client_reply)
  if not general or general == "" then
    general = json.decode(player.default_reply)
  end
  return general
  -- room:changeHero(player, general, true, false, true)
end

return LUltiity